package com.juzipi.demo.singleton.type2;

public class SingletonTest2 {
    public static void main(String[] args) {
        //测试
        Singleton instance = Singleton.getInstance();
        Singleton instance1 = Singleton.getInstance();
        System.out.println(instance.equals(instance1));//true
        System.out.println(instance.hashCode());
        System.out.println(instance1.hashCode());

    }
}
//饿汉式（静态变量）
class Singleton{
    //1.先将构造器私有化,外部不能new
    private Singleton(){

    }

    //2.本类内部创建对象实例
    private static Singleton instance;

    //在静态代码块中创建单例对象
    static {
        instance = new Singleton();
    }
    //3.对外提供一个公有的静态方法，返回实例对象
    public static Singleton getInstance(){
        return instance;
    }

}
